package com.tianyi.sfzy1.controller;

import com.tianyi.sfzy1.domain.Coursediscussion;
import com.tianyi.sfzy1.domain.Coursefavorite;
import com.tianyi.sfzy1.service.CoursefavoriteService;
import com.tianyi.sfzy1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/coursefavorite")
public class CoursefavoriteController {
    @Autowired
    private CoursefavoriteService coursefavoriteService;


    //添加
    @PostMapping
    public R insert(@RequestBody Coursefavorite record) {
        System.out.println(record);
        return R.ok( coursefavoriteService.insert(record));
    }


    //查询所有
    @GetMapping
    public R findAll(){
        return R.ok(coursefavoriteService.findAll());
    }


    //根据id查询
    @GetMapping("/id")
    public Coursefavorite selectByPrimaryKey( int favoriteId){
        System.out.println(coursefavoriteService);
        return coursefavoriteService.selectByPrimaryKey(favoriteId);
    }

    //修改
    @PutMapping
    public R updateByPrimaryKey(Coursefavorite record) {
        System.out.println(record);
        return R.ok(coursefavoriteService.updateByPrimaryKey(record));
    }

    //删除
    @DeleteMapping
    public R deleteByPrimaryKey( int favoriteId){
        return R.ok(coursefavoriteService.deleteByPrimaryKey(favoriteId));
    }

    /**
     * 小程序增加新收藏
     */
    @PostMapping("/one")
    public R insertOne(int memberId,int courseId){
        int i=coursefavoriteService.insertOne(memberId,courseId);
        if (i>0){
            return R.ok(null);
        }else {
            return R.fail("添加失败");
        }
    }

    /**
     * 小程序删除收藏
     */
    @DeleteMapping("/delone")
    public R deleteOne(int memberId,int courseId){
        int i=coursefavoriteService.deleteOne(memberId,courseId);
        if (i>0){
            return R.ok(null);
        }else {
            return R.fail("删除失败");
        }
    }

    /**
     * 小程序根据用户和课程id查询
     */
    @GetMapping("/find")
    public R find(int memberId,int courseId){
        Coursefavorite coursefavorite=coursefavoriteService.find(memberId, courseId);
        return R.ok(coursefavorite);
    }
}
